OPTIONS NOFMTERR MPRINT PS=60 LS=80 sasautos =macro;
FILENAME macro "/disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/macro";


libname r '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru';
libname wk2 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2007';
libname wk '/disk/agebulk2/medicare.work/newhouse-DUA19577';
libname perm '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab';
libname tab '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/price_tables';
libname pm2  '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/patient_cohorts';
libname perm7 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/2007';
libname hed '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/MA_files';
libname pb '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/partb/2007';
libname ip '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/ip/2007';
libname op '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/op/2007';


***RRU DIABETES RESULTS;
data rru (drop = esrd);
set perm7.diab2007 (keep = ed_vis ip_vis ip_cost em_cost surg_cost
 comorb type bene_id aids organ esrd cancer hmocov enrld);
if aids ~= 1 and  organ ~= 1 and esrd ~= 1 and cancer ~= 1;
if enrld in (1,2) and hmocov = "00";
proc sort;
 by bene_id;

***DENOM TO GET RACE;
data den;
 length racec $ 10;
 set wk2.results07 (keep= bene_id enrld sex county
 esrd hmocov nurs age race state zip5 months);
 where age >= 65 and age <= 75 and enrld in (1,2) and esrd ~= "Y";
 if hmocov = 0 and esrd ~= "Y" and nurs ~in ("both", "long");
 if state > "53" or state in ("40", "48") then delete;
racec = race;
if racec = "0" then racec = "0.Unknown";
if racec = "1" then racec = "1.White";
if racec = "2" then racec = "2.Black";
if racec = "3" then racec = "3.Other";
if racec = "4" then racec = "4.Asian";
if racec = "5" then racec = "5.Hispanic";
if racec = "6" then racec = "6.Native";

if racec in ("0.Unknown", "4.Asian", "6.Native") then racec = "3.Other";

length division $ 15;

ffs_count = months/12;

length gender $ 8;
gender = sex;

if gender = "1" then gender = "1.Male";
if gender = "2" then gender = "2.Female";

if age >= 65 and age <= 70 then newagec = "1. 65-70";
else if age > 70 and age <= 75 then newagec = "2. 71-75";

if zip5 = "00000" then zip5 = "00001";
if zip5 = "99999" then zip5 = "99998";

link = compress(racec||gender||newagec||zip5);


if county = "999" then county = "998";
if zip5 = "00000" then zip5 = "00001";
if zip5 = "99999" then zip5 = "99998";
link_cty = compress(racec||gender||newagec||county||state);
link_st = compress(racec||gender||newagec||state);


proc sort;
 by bene_id;



***SPLIT INTO CONDITION CATEGORIES;

%macro g (gp);
data c0 c1;
 merge den (in=d) rru (in=r);
 by bene_id;
 if d = 1;
 if surg_cost = . then surg_cost = 0;
 if ip_cost = . then ip_cost = 0;
 if em_cost = . then em_cost = 0;
 if ip_vis = . then ip_vis = 0;
 if ed_vis = . then ed_vis = 0;

if type = 1 and gender = "1.Male" and comorb = 0 then grp = "1mn";
if type = 1 and gender = "1.Male" and comorb = 1 then grp = "1mc";
if type = 2 and gender = "1.Male" and comorb = 0 then grp = "2mn";
if type = 2 and gender = "1.Male" and comorb = 1 then grp = "2mc";
if type = 1 and gender = "2.Female" and comorb = 0 then grp = "1fn";
if type = 1 and gender = "2.Female" and comorb = 1 then grp = "1fc";
if type = 2 and gender = "2.Female" and comorb = 0 then grp = "2fn";
if type = 2 and gender = "2.Female" and comorb = 1 then grp = "2fc";

if grp = "&gp" then output c1;
 else output c0;
run;


***SUM CATGORIES BY CONDITION;
%macro m(n);
proc sql;
 create table c&n. as select link, sum(ffs_count) as sm_ffs_count_c&n.,
 sum(ed_vis) as sm_ed_vis_c&n.,
 sum(ip_vis) as sm_ip_vis_c&n.,
 sum(ip_cost) as sm_ip_cost_c&n.,
 sum(em_cost) as sm_em_cost_c&n.,
 sum(surg_cost) as sm_surg_cost_c&n.,
 var(ed_vis) as vr_ed_vis_c&n.,
 var(ip_vis) as vr_ip_vis_c&n.,
 var(ip_cost) as vr_ip_cost_c&n.,
 var(em_cost) as vr_em_cost_c&n.,
 var(surg_cost) as vr_surg_cost_c&n.,
 count(link) as mem_c&n.
 from c&n. group by link
 order by link;
%mend m;

%m(0); %m(1);
run;


data mg;
 merge c0 (keep = link sm_ffs_count_c0 mem_c0) c1;
 by link;
run;


data mg;
 set mg;

array vars(12) sm_ffs_count_c1 -- mem_c1;
do j = 1 to 12;
 if vars(j) = . then vars(j) = 0;
end;

 ffs_count = sm_ffs_count_c0 + sm_ffs_count_c1;

***CALCULATE PERCENTS;
pct_c0 = sm_ffs_count_c0/ffs_count;
pct_c1 = sm_ffs_count_c1/ffs_count;

 ed_pct_c1 = sm_ed_vis_c1/ffs_count;

 ip_pct_c1 = sm_ip_vis_c1/ffs_count;

 em_cost_pct_c1 = sm_em_cost_c1/ffs_count;

 ip_cost_pct_c1 = sm_ip_cost_c1/ffs_count;

 surg_cost_pct_c1 = sm_surg_cost_c1/ffs_count;

proc sort;
 by link;


proc sql;
 create table org
 as select distinct org_id, id2 from r.hmo07rru2
 order by id2;


%macro mm (n);
***HMO ENROLLMENT;
%do num = 1 %to &n.;

data hmo;
 length link $ 50;
 set r.hmo07rru2;
 where id2 = &num.;
 pats = months/12;
 drop hmo_count;


proc sql;
 create table hmo as select
 link, sum(pats) as hmo_count from hmo
 group by link
 order by link;

data mg2;
 merge mg (in=m) hmo (in=h);
 by link;
 
if hmo_count = . then hmo_count = 0;

if hmo_count > 0;

w_scost_var_c1 = vr_surg_cost_c1 * hmo_count;

w_icost_var_c1 = vr_ip_cost_c1 * hmo_count;

w_ecost_var_c1 = vr_em_cost_c1 * hmo_count;

w_ivis_var_c1 = vr_ip_vis_c1 * hmo_count;

w_evis_var_c1 = vr_ed_vis_c1 * hmo_count;

w_e_vis_c1 = ed_pct_c1 * hmo_count;

w_i_vis_c1 = ip_pct_c1 * hmo_count;

w_e_cost_c1 = em_cost_pct_c1 * hmo_count;

w_i_cost_c1 = ip_cost_pct_c1 * hmo_count;

w_s_cost_c1 = surg_cost_pct_c1 * hmo_count;

w_pct1 = pct_c1 * hmo_count;

if w_i_vis_c1 > 0 then ffs_count_ivis = sm_ffs_count_c1; else ffs_count_ivis = 0;
if w_e_vis_c1 > 0 then ffs_count_evis = sm_ffs_count_c1; else ffs_count_evis = 0;
if w_i_cost_c1 > 0 then ffs_count_icost = sm_ffs_count_c1; else ffs_count_icost = 0;
if w_s_cost_c1 > 0 then ffs_count_scost = sm_ffs_count_c1; else ffs_count_scost = 0;
if w_e_cost_c1 > 0 then ffs_count_ecost = sm_ffs_count_c1; else ffs_count_ecost = 0;



run;

proc printto print='dum.lst';
proc surveymeans  data=mg2;
 var w_i_cost_c1 w_pct1
 w_e_cost_c1 w_pct1
 w_s_cost_c1 w_pct1
 w_i_vis_c1 w_pct1
 w_e_vis_c1 w_pct1;

 ratio w_i_vis_c1/w_pct1;
 ratio w_e_vis_c1/w_pct1;
 ratio w_i_cost_c1/w_pct1;
 ratio w_e_cost_c1/w_pct1;
 ratio w_s_cost_c1/w_pct1;


 ods output statistics=mystat ratio=rat;
run;
proc printto;
run;

proc sql;
 create table id as select distinct 
 id2, org_id from r.hmo07rru2
 order by id2;

data rat;
 length id2 4;
 set rat;
 id2 = "&num.";
proc sort;
 by id2;

data rat;
 merge rat (in=r) id;
 by id2;
 if r = 1;

proc append data=rat base=allrat;

proc sql;
 create table temp as select sum(hmo_count) as hmo_count,
sum(w_s_cost_c1) as w_s_cost_c1, sum(w_i_cost_c1) as w_i_cost_c1,
sum(w_e_cost_c1) as w_e_cost_c1,
sum(w_i_vis_c1) as w_i_vis_c1,
sum(w_e_vis_c1) as w_e_vis_c1,
sum(w_pct1) as w_pct1,
sum(sm_ffs_count_c0) as sm_ffs_count_c0,
sum(sm_ffs_count_c1) as sm_ffs_count_c1,
sum(ffs_count) as ffs_count,
max(ffs_count_ivis) as max_ffs_count_ivis,
max(ffs_count_evis) as max_ffs_count_evis,
max(ffs_count_icost) as max_ffs_count_icost,
max(ffs_count_ecost) as max_ffs_count_ecost,
max(ffs_count_scost) as max_ffs_count_scost,
sum(pct_c0) as pct_c0,
sum(pct_c1) as pct_c1,
sum(sm_ed_vis_c1) as sm_ed_vis_c1,
sum(sm_ip_vis_c1) as sm_ip_vis_c1,
sum(sm_ip_cost_c1) as sm_ip_cost_c1,
sum(sm_em_cost_c1) as sm_em_cost_c1,
sum(sm_surg_cost_c1) as sm_surg_cost_c1,
sum(w_evis_var_c1) as w_evis_var_c1,
sum(w_ivis_var_c1) as w_ivis_var_c1,
sum(w_scost_var_c1) as w_scost_var_c1,
sum(w_ecost_var_c1) as w_ecost_var_c1,
sum(w_icost_var_c1) as w_icost_var_c1
from mg2;


data temp;
 set temp;
  length id2 4;

grp = "&gp";

c1 = w_pct1/hmo_count;

icost_var_c1= w_icost_var_c1/hmo_count;

scost_var_c1= w_scost_var_c1/hmo_count;

ecost_var_c1= w_ecost_var_c1/hmo_count;

ivis_var_c1= w_ivis_var_c1/hmo_count;

evis_var_c1= w_evis_var_c1/hmo_count;

ed_vis_c1 = w_e_vis_c1/hmo_count/c1;

ip_vis_c1 = w_i_vis_c1/hmo_count/c1;

surg_cost_c1 = w_s_cost_c1/hmo_count/c1;

ip_cost_c1 = w_i_cost_c1/hmo_count/c1;

em_cost_c1 = w_e_cost_c1/hmo_count/c1;

id2 = "&num.";
run;
proc sort;
 by id2;

data temp;
 merge temp (in=r) id;
 by id2;
 if r = 1;

proc append base=allset data = temp;



%end;

%mend mm;

%mm(317)


proc print data=allset;
 title "allset";

data perm7.plan_res2;
 set allset;
run;


data perm7.plan_se;
 set allrat;

data perm7.plan_res;
 set allset;
run;

%mend g;

%g(1mn)
%g(1mc)
%g(1fn)
%g(1fc)
%g(2mn)
%g(2mc)
%g(2fn)
%g(2fc)


